package moltencore.game.loader;

import moltencore.game.Grid;
import moltencore.game.bubble.Bubble;
import moltencore.game.bubble.BubbleRandomizer;
import moltencore.game.bubble.Type;
import org.apache.log4j.Logger;
import spark.utility.IncrementalShortIdFactory;
import spark.utility.ShortIdFactory;

import java.util.LinkedList;

public class GridGenerator {
	private static Logger logger = Logger.getLogger(GridGenerator.class);

	private LinkedList<Bubble> gridBubbles = new LinkedList<Bubble>();

	public Grid copyLastGenerated() {
		Grid grid = new Grid();
		if(gridBubbles == null) {
			return grid;
		}
		for(Bubble bubble : gridBubbles) {
			Bubble copy = new Bubble(bubble.getType(), bubble.getBubbleId());
			copy.setAttachedToWorld(bubble.isAttachedToWorld());
			grid.snapToGrid(copy, bubble.getGridPosition().x, bubble.getGridPosition().y);
		}
		return grid;
	}

	public void generateGrid(BubbleRandomizer randomizer, Level level, ShortIdFactory bubbleIdFactory) {
		gridBubbles.clear();
		for(Bubble fillBubble : level.getFillBubbles()) {
			Bubble gridBubble = new Bubble(randomizer.randomBubbleType(), bubbleIdFactory.getNextId());
			gridBubble.getGridPosition().x = fillBubble.getGridPosition().x;
			gridBubble.getGridPosition().y = fillBubble.getGridPosition().y;
			for(Bubble pushBubble : level.getPushBubbles()) {
				if(gridBubble.getGridPosition().equals(pushBubble.getGridPosition())) {
					gridBubble.setAttachedToWorld(true);
				}
			}
			gridBubbles.add(gridBubble);
		}
	}

	public static Grid get(int grid) {
		switch(grid) {
			case 0:
				return get_0();
			case 1:
				return get_1();
			default:
				logger.fatal("No grid " + grid);
				return null;
		}
	}

	private static Grid get_0() {
		int fieldWith = 8;
		Grid grid = new Grid();
		Bubble bubble = null;

		IncrementalShortIdFactory idFactory = new IncrementalShortIdFactory();
		idFactory.getNextId();

		for(int row = 0; row < 4; row++) {
			for(int col = 0; col < fieldWith - ((row % 2 == 0) ? 0 : 1); col++) {
				switch((row + col) % 5) {
					case 0:
						bubble = new Bubble(Type.Red, (short) -idFactory.getNextId());
						break;
					case 1:
						bubble = new Bubble(Type.Green, (short) -idFactory.getNextId());
						break;
					case 2:
						bubble = new Bubble(Type.Blue, (short) -idFactory.getNextId());
						break;
					case 3:
						bubble = new Bubble(Type.Orange, (short) -idFactory.getNextId());
						break;
					case 4:
						bubble = new Bubble(Type.Yellow, (short) -idFactory.getNextId());
						break;
				}
				if(row == 0) {
					bubble.setAttachedToWorld(true);
				}
				if(bubble.getBubbleId() == -29) {
					System.out.println();
				}
				grid.snapToGrid(bubble, col, -row);
			}
		}
		//bubble = new Bubble(Type.Orange, (short) -idFactory.getNextId());
		//grid.snapToGrid(bubble, 5, -4);
		//bubble = new Bubble(Type.Yellow, (short) -idFactory.getNextId());
		//grid.snapToGrid(bubble, 5, -5);
		return grid;
	}

	private static Grid get_1() {
		Grid grid = new Grid();

		return grid;
	}
}